package com.supermap.dataservice.cloud.project;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.project.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author aqua
 * @description
 * @date 2019年11月05 17:24
 * @email 1301584033@qq.com
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Override
    public JSONUtils getUserParamList(String areaParm, Integer projectId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(projectMapper.getUserParamList(areaParm,projectId));
    }

    @Override
    public JSONUtils getPageProjectList(String name,String userId, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String,Object>> list=projectMapper.getPageProjectList(name,userId,page,size);
        list.stream().forEach(map ->{
            map.put("plans",projectMapper.getPlansList((Integer) map.get("id")));
            map.put("expends",projectMapper.getExpendsList((Integer) map.get("id")));
        });
        return new JSONUtils().setData(list).setCountSum(projectMapper.getPageProjectListCount(name,userId));
    }

    @Override
    public JSONUtils getExpendList() {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(projectMapper.getExpendList());
    }

    @Override
    public JSONUtils putProject(Project project) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String date=DateUtil.now();
        project.setCtime(date);
        projectMapper.putProject(project);
        List<ProjectPlan> plans= project.getPlans();
        Integer id=projectMapper.getId();
        List<ExpendStype> expendStypes= project.getExpendStypes();
        plans.stream().forEach(plan -> {plan.setProjectid(id);plan.setCtime(date);});
        projectMapper.putPlans(plans);
        projectMapper.putProjectUsers(project.getCusers(),id);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delProject(Integer projectId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        projectMapper.delProject(projectId);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delProjectPlan(Integer PlanId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        projectMapper.delProjectPlan(PlanId);
        return new JSONUtils();
    }

    @Override
    public JSONUtils postProject(Project project) {
        return new JSONUtils().setMsg("功能正在做，预留接口老铁");
    }

    @Override
    public JSONUtils postProjectPlan(ProjectPlan plan) {
        return new JSONUtils().setMsg("功能正在做，预留接口老铁");
    }

    @Override
    public JSONUtils getPageProjectSubmitList(Integer projectId,String userId, Integer planid,String bDate,String eDate, String state,String stype, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");


        Map<String,Object> map= projectMapper.getPageProjectSubmitListCount(projectId,userId,planid,bDate,eDate,state,stype);
        Double price= (Double)map.get("price");
        Double numprice= (Double)map.get("numprice");
        BigDecimal countNum= (BigDecimal) map.get("countNum");
        return new JSONUtils().setData(projectMapper.getPageProjectSubmitList(projectId,userId,planid,bDate,eDate,state,stype,page,size))
                .setCountSum(countNum.intValue()).setMsg("实际总价格:A"+price+"B核定总价格:C"+numprice+"D这个英文是帮助你截取实际没用");
    }

    @Override
    public JSONUtils postSubmitList(SubmitFile submitFile) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String date=DateUtil.now();
        submitFile.setCtime(date);
        String path="/mnt/ChinaApp/MicroService/static/area/keyan/"+submitFile.getFileName();
        //String path="D:/"+submitFile.getFileName();
        ExcelReader reader = ExcelUtil.getReader(path);
        List<List<Object>> readAll = reader.read().stream().skip(1).collect(Collectors.toList());
        projectMapper.postSubmitFile(submitFile);
        Integer id=projectMapper.getId();
        projectMapper.putSubmitList(readAll,id);
        //解析
        return new JSONUtils();
    }

    @Override
    public JSONUtils getPageProjectChengguoList(Integer projectId, Integer typeid, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String,Object>> list= projectMapper.getPageProjectachievementList(projectId,typeid,page,size);
        list.stream().forEach(map ->{
            map.put("fileList", projectMapper.getAChFIleList((Integer) map.get("id")));
        });
        Integer count= projectMapper.getPageProjectachievementListCount(projectId,typeid);
        return new JSONUtils().setData(list).setCountSum(count);
    }

    @Override
    public JSONUtils putAchievement(Achievement achievement) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String date=DateUtil.now();
        achievement.setCtime(date);
        projectMapper.putAchievement(achievement);
        Integer id=projectMapper.getId();
        projectMapper.putAchievementFiles(achievement.getFileNames(),achievement.getFileTitles(),id);
        return new JSONUtils();
    }

    @Override
    public JSONUtils getPageAreaCropList(String areaCode, String cropName, Integer yer, String xiangzheng, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Map<String,Object> map= projectMapper.getPageAreaCropListCount(areaCode,cropName,yer,xiangzheng);
        BigDecimal zongcan= (BigDecimal)map.get("zongcan");
        BigDecimal zongmu= (BigDecimal)map.get("zongmu");
        long countNum= (long) map.get("countNum");
        return new JSONUtils().setData(projectMapper.getPageAreaCropList(areaCode,cropName,yer,xiangzheng,page,size))
                .setCountSum((int) countNum).setMsg("总产量:A"+zongcan.intValue()+"B总面积:C"+zongmu.intValue()+"D这个英文是帮助你截取实际没用");
    }

    @Override
    public JSONUtils putAreaCrop(AreaCrop areaCrop) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String date=DateUtil.now();
        areaCrop.setCdete(date);
        String path="/mnt/ChinaApp/MicroService/static/area/load/"+areaCrop.getFileName();
        //String path="D:/"+areaCrop.getFileName();
        ExcelReader reader = ExcelUtil.getReader(path);
        List<List<Object>> readAll = reader.read().stream().skip(1).collect(Collectors.toList());
        readAll.stream().forEach(list ->{
                list.add(8,projectMapper.getAreaCode(list.get(3).toString().trim()));
        });
        projectMapper.insertAreaCropFile(areaCrop);
        Integer id=projectMapper.getId();
        projectMapper.putAreaCrop(readAll,id);
        return new JSONUtils();
    }


}
